package com.sykj.weichat.web;

import java.math.BigDecimal;
import com.sykj.weichat.service.BisBeautyRecordService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.sykj.weichat.service.SysParamService;
import com.sykj.weichat.service.BisProjectRecordService;
import com.sykj.weichat.service.BisBusinessOrderService;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.weichat.model.BisCustomUser;
import com.sykj.weichat.model.BisNurseBag;
import com.sykj.weichat.model.BisNurseProject;
import com.sykj.weichat.model.BisOrderDeposit;
import com.sykj.weichat.model.BisProjectRecord;
import com.sykj.weichat.model.BisShop;
import com.sykj.weichat.model.SysParam;
import com.sykj.util.ResultBody;
import com.alibaba.druid.stat.TableStat.Mode;
import com.google.common.collect.Lists;
import com.sykj.weichat.model.BisBagProject;
import com.sykj.weichat.model.BisBagRecord;
import com.sykj.weichat.model.BisBeautyConfig;
import com.sykj.weichat.model.BisBeautyRecord;
import com.sykj.weichat.model.BisBusinessOrder;
import com.sykj.weichat.service.BisBagProjectService;
import com.sykj.weichat.service.BisBagRecordService;
import com.sykj.weichat.service.BisBeautyConfigService;
import com.sykj.weichat.service.BisCustomUserService;
import com.sykj.weichat.service.BisNurseBagService;
import com.sykj.weichat.service.BisNurseProjectService;
import com.sykj.weichat.service.BisOrderDepositService;
import com.sykj.weichat.service.BisShopService;
/**
 * 
 * 类名:com.sykj.weichat.web.WeichatOrderController
 * 描述:微信订单相关
 * @author 张孝柱
 * 创建时间:2018年9月11日 
 * 修改说明:
 */

@Controller
@RequestMapping("weichat/order")
public class WeichatOrderController {
	@Autowired
	BisBeautyConfigService BisBeautyConfigService;
	@Autowired
	BisCustomUserService BisCustomUserService;
	@Autowired
	BisBeautyRecordService BisBeautyRecordService;
	@Autowired
	BisBusinessOrderService BisBusinessOrderService;
	@Autowired
	BisNurseBagService bisnursebag;
	@Autowired
	BisShopService BisShopService;
	@Autowired
	BisOrderDepositService BisOrderDepositService;
	@Autowired
	BisBagRecordService BisBagRecordService;
	@Autowired
	BisBagProjectService BisBagProjectService;
	@Autowired
	BisProjectRecordService BisProjectRecordService;
	@Autowired
	SysParamService SysParamService;
	@Autowired 
	BisNurseProjectService BisNurseProjectService;
	/**
	 * 
	 * 简要说明：查询美丽币的购买信息
	 * @author 张孝柱
	 * 创建时间:2018年9月11日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("/listBeautyInfo")
	public String listBeautyInfo(String shop_id,Model model){
		List<BisBeautyConfig> bis = new ArrayList<BisBeautyConfig>();
		List<BisBeautyConfig> getall = BisBeautyConfigService.getall();
		SysParam getall2 = SysParamService.getall();
		int Param_value = Integer.parseInt(getall2.getParam_value()) ;
		System.out.println(Param_value+"******************************");
		for (BisBeautyConfig bisBeautyConfig : getall) {  
			String config_id = bisBeautyConfig.getConfig_id();
			Integer buy_num = bisBeautyConfig.getBuy_num();
			Integer give_num = bisBeautyConfig.getGive_num();
			BigDecimal b1 = new BigDecimal(Double.toString(buy_num));
	        BigDecimal b2 = new BigDecimal(Double.toString(Param_value));
	        double total_price= b1.multiply(b2).doubleValue();
			BisBeautyConfig config = new BisBeautyConfig();
			config.setConfig_id(config_id);
			config.setBuy_num(buy_num);
			config.setGive_num(give_num);
			config.setTotal_price(total_price);
			bis.add(config);
		}
		model.addAttribute("shop_id", shop_id);
		model.addAttribute("beautyList", bis);
		return "/weichat/my/buyBeauty.jsp";
	}

	/**
	 * 
	 * 简要说明：保存美丽币购买信息
	 * 编写者：张孝柱
	 * 创建时间:2018年9月11日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("saveBuyBeautyOrder")
	@ResponseBody
	public String saveBuyBeautyOrder(BisBeautyRecord bisBeautyRecord,BisBeautyConfig bisBeautyConfig,HttpSession session,BisCustomUser bisCustomUser,BisBusinessOrder bisBusinessOrder){
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		String config_id = bisBeautyConfig.getConfig_id();
		BisBeautyConfig users = BisBeautyConfigService.getUsers(config_id);
		Integer buy_num = users.getBuy_num();
		Integer give_num = users.getGive_num();
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss"); 
		SysParam getall2 = SysParamService.getall();
		int Param_value = Integer.parseInt(getall2.getParam_value()) ;
		//保存美颜币订单
		String format2 = df1.format(day);
		String format = df.format(day);
		bisBusinessOrder.setOrder_id(format2);
		bisBusinessOrder.setCustom_user_id(custom_user_id);
		bisBusinessOrder.setOrder_money(buy_num*Param_value);
		bisBusinessOrder.setPay_time(format);
		bisBusinessOrder.setCreate_time(format);
		int addbeauty = BisBusinessOrderService.addbeauty(bisBusinessOrder);
		if (addbeauty>0) {
			//保存美颜币流水
			bisBeautyRecord.setCustom_user_id(custom_user_id);
			bisBeautyRecord.setPay_time(format);
			bisBeautyRecord.setBeauty_num(buy_num*Param_value);
			bisBeautyRecord.setGive_num(give_num);
			int insert = BisBeautyRecordService.insert(bisBeautyRecord);
			//修改个人美颜币数量
			bisCustomUser.setCustom_user_id(custom_user_id);
			bisCustomUser.setBeauty_num((buy_num+give_num));
			BisCustomUserService.updatebeauty_num(bisCustomUser);
			result = String.format(result,"1", "Success!");
		}else {
			result = String.format(result,"0", "Failed!");
		}
		return result;
	}
	/**
	 * 
	 * 简要说明：初始化护理订单
	 * 编写者：张孝柱
	 * 创建时间:2018年9月14日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("/goMyOrder")
	public String goMyOrder(String index,Model model){
		model.addAttribute("index", index);
		return "/weichat/my/order/myOrder.jsp";
		
	}
	/**
	 * 
	 * 简要说明：搜索护理订单
	 * 编写者：张孝柱
	 * 创建时间:2018年9月14日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("/listNurseOrder")
	@ResponseBody
	public ResultBody listNurseOrder(HttpSession session){
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		List<BisBusinessOrder> getall = BisBusinessOrderService.getall(custom_user_id);
		return ResultBody.getData(getall.size(), getall, "Success!");
		
	}
	/**
	 * 
	 * 简要说明：查看护理订单详情
	 * 编写者：张孝柱
	 * 创建时间:2018年9月14日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value="showNurseOrderDetail")
	public String showNurseOrderDetail(String order_id,Model model){
		BisBusinessOrder order = BisBusinessOrderService.getOrder(order_id);
		String order_status = order.getOrder_status();
		model.addAttribute("nurseProjectPO", order);
		System.out.println(order_status+".............................");
		if (order_status.equals("1")) {//已预约
			System.out.println(1);
			return "/weichat/my/order/showSubscribeOrderDetail.jsp";
			
		}else if (order_status.equals("2")) {//服务中
			return "/weichat/my/order/showServerOrderDetail.jsp";
		}else if (order_status.equals("3")) {//待支付
			return "/weichat/my/order/showCompleteOrderDetail.jsp";
		}else if (order_status.equals("4")) {//已支付
			return "/weichat/my/order/showPayOrderDetail.jsp";
		}else if (order_status.equals("5")) {//已完成
			return "/weichat/my/order/showCompleteOrderDetail.jsp";
		}else if (order_status.equals("6")) {//已过期
			return "/weichat/my/order/showCompleteOrderDetail.jsp";
		}else {//已撤销
			return "/weichat/my/order/showCancelOrderDetail.jsp";
		}		
	}
	/**
	 * 
	 * 简要说明：取消订单
	 * 编写者：张孝柱
	 * 创建时间:2018年9月17日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "cancelOrder")
	@ResponseBody
	public String cancelOrder(String order_id){
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		int updatestatus = BisBusinessOrderService.updatestatus(order_id);
		if (updatestatus>0) {
			result = String.format(result,"1", "Success!");
		}else {
			result = String.format(result,"0", "Failed!");
		}
		return result;
		
		
	}
	/**
	 * 
	 * 简要说明：搜索护理订单
	 * 编写者：张孝柱
	 * 创建时间:2018年9月17日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "listBagOrder")
	@ResponseBody
	public ResultBody listBagOrder(HttpSession session){
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		List<BisBusinessOrder> showBagOrderDetail = BisBusinessOrderService.showBagOrderDetail(custom_user_id);
		return ResultBody.getData(showBagOrderDetail.size(), showBagOrderDetail, "Success!");
		
	}
	/**
	 * 
	 * 简要说明：查看护理订单详情
	 * 编写者：张孝柱
	 * 创建时间:2018年9月18日 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value="showBagOrderDetail")
	public String showBagOrderDetail(String order_id,Date overdue_date,Model model){
		BisNurseBag nurseBagPO = bisnursebag.nurseBagPO(order_id);
		model.addAttribute("nurseBagPO", nurseBagPO);
		model.addAttribute("overdue_date", overdue_date);
		return "/weichat/my/order/showBagOrderDetail.jsp";
	}
	/**
	 * 
	 * 简要说明： 保存app预约订单 编写者：陈骑元 创建时间：2017年6月3日 下午9:33:10
	 * 
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value="saveAppSubscribeOrder")
	@ResponseBody
	public String saveAppSubscribeOrder(String project_id,String shop_id, @DateTimeFormat(pattern="yyyy-MM-dd") Date subscribe_time,String order_content,double order_money,HttpSession session,BisBusinessOrder bisBusinessOrder,BisOrderDeposit bisOrderDeposit){
		Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String format = df.format(day);
		bisBusinessOrder.setCreate_time(format);
		SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss"); 
		String format2 = df1.format(day);
		bisBusinessOrder.setOrder_id(format2);
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		bisBusinessOrder.setCustom_user_id(custom_user_id);
		bisBusinessOrder.setProject_id(project_id);
		bisBusinessOrder.setShop_id(shop_id);
		bisBusinessOrder.setSubscribe_time(subscribe_time);
		bisBusinessOrder.setPay_time(format);
		bisBusinessOrder.setOrder_content(order_content);
		int addproject = BisBusinessOrderService.addproject(bisBusinessOrder);
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		if (addproject>0) {
			bisOrderDeposit.setDeposit_id(format2);
			bisOrderDeposit.setOrder_id(format2);
			bisOrderDeposit.setPay_time(format);
			bisOrderDeposit.setCustom_user_id(custom_user_id);
			BisOrderDepositService.insert(bisOrderDeposit);
			result = String.format(result,"1", "Success!");
			/*String.f*/
		}else {
			result = String.format(result,"0", "Failed!");
		}
		return result;
		
	}
	/**
	 * 
	 * 简要说明：保存礼包购买信息
	 * 编写者：张孝柱
	 * 创建时间：2018年9月20日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("saveBuyBagOrder")
	@ResponseBody
	public String saveBuyBagOrder(String bag_id,double order_money,int buy_num,String order_content,HttpSession session,BisBusinessOrder bisBusinessOrder,BisBagRecord bagRecord,BisProjectRecord bisProjectRecord,BisCustomUser bisCustomUser,BisNurseBag bag){
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		BisCustomUser users = BisCustomUserService.getUsers(custom_user_id);
		Date day = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String format = df.format(day);
		bisBusinessOrder.setCreate_time(format);
		SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		System.out.println("******"+users.getBeauty_num()+"------"+order_money);
		if (users.getBeauty_num()>order_money) {
			String format2 = df1.format(day);
			//添加购买订单
			bisBusinessOrder.setCustom_user_id(custom_user_id);
			bisBusinessOrder.setOrder_id(format2);
			bisBusinessOrder.setOrder_content(order_content);
			bisBusinessOrder.setOrder_money(order_money);
			bisBusinessOrder.setExtend_beauty_num((int) order_money);
			bisBusinessOrder.setPay_time(format);
			bisBusinessOrder.setCreate_time(format);
			bisBusinessOrder.setBuy_num(buy_num);
			int addbag = BisBusinessOrderService.addbag(bisBusinessOrder);
			//添加购买礼包
			bagRecord.setBag_id(bag_id);
			bagRecord.setCustom_user_id(custom_user_id);
			bagRecord.setBuy_num(buy_num);
			bagRecord.setCreate_time(day);
			bagRecord.setOrder_id(format2);
			BisBagRecordService.addbag(bagRecord);
			List<BisBagProject> getusers = BisBagProjectService.getusers(bag_id);
			for (BisBagProject bisBagProject : getusers) {
				//添加项目使用记录表
				String project_id = bisBagProject.getProject_id();
				bisProjectRecord.setProject_id(project_id);
				bisProjectRecord.setBag_record_id(bag_id);
				bisProjectRecord.setDraw_time(day);
				BisProjectRecordService.add(bisProjectRecord);
				
			}
				bisCustomUser.setBeauty_num((int) order_money);
				bisCustomUser.setCustom_user_id(custom_user_id);
				BisCustomUserService.beauty_num(bisCustomUser);
				bag.setBag_id(bag_id);
				bag.setRemain_num(buy_num);;
				bisnursebag.update(bag);
				result = String.format(result,"1", "Success！");
		}else {
			result = String.format(result,"0", "美颜币不足，请充值");
		}
		
		return result;
		
	}
	/**
	 * 
	 * 简要说明：查看护理订单详情
	 * 编写者:张孝柱
	 * 创建时间：2018年9月27日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value="goPayOrder")
	public String goPayOrder(String order_id,HttpSession session,BisBusinessOrder bisBusinessOrder,Model model,BisNurseProject bisNurseProject){
		bisBusinessOrder.setOrder_id(order_id);
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		bisBusinessOrder.setCustom_user_id(custom_user_id);
		BisBusinessOrder queryPay = BisBusinessOrderService.queryPay(bisBusinessOrder);
		String project_id = queryPay.getProject_id();
		bisNurseProject.setProject_id(project_id);
		bisNurseProject.setCustom_user_id(custom_user_id);
		List<BisNurseProject> queryPayProject = BisNurseProjectService.queryPayProject(bisNurseProject);
		int count=queryPayProject.size();
		model.addAttribute("orderDto", queryPay);
		model.addAttribute("projectList", queryPayProject);
		model.addAttribute("count", count);
		return "/weichat/my/order/payOrder.jsp";
	}

/**
 * 
 * 简要说明：查看礼包订单详情
 * 编写者:张孝柱
 * 创建时间：2018年9月27日
 * @param 说明
 * @return 说明
 */
@RequestMapping(value="goPayOrder1")
public String goPayOrder1(String order_id,HttpSession session,BisBusinessOrder bisBusinessOrder,Model model,BisNurseProject bisNurseProject){
	bisBusinessOrder.setOrder_id(order_id);
	String custom_user_id = (String) session.getAttribute("custom_user_id");
	bisBusinessOrder.setCustom_user_id(custom_user_id);
	BisBusinessOrder queryPay = BisBusinessOrderService.bag(bisBusinessOrder);
	String project_id = queryPay.getProject_id();
	bisNurseProject.setProject_id(project_id);
	bisNurseProject.setCustom_user_id(custom_user_id);
	List<BisNurseProject> queryPayProject = BisNurseProjectService.queryPayProject(bisNurseProject);
	int count=queryPayProject.size();
	model.addAttribute("orderDto", queryPay);
	model.addAttribute("projectList", queryPayProject);
	model.addAttribute("count", count);
	return "/weichat/my/order/payOrder.jsp";
}
	/**
	 * 
	 * 简要说明：美颜币支付订单
	 * 编写者：张孝柱
	 * 创建时间：2018年9月27日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "beautyPayOrder")
	@ResponseBody
	public String beautyPayOrder(String order_id,Integer beauty_num,BisCustomUser bisCustomUser,BisBusinessOrder bisBusinessOrder,HttpSession session){
		bisBusinessOrder.setOrder_id(order_id);
		bisBusinessOrder.setExtend_beauty_num(beauty_num);
		bisBusinessOrder.setExtend_income(beauty_num);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		bisBusinessOrder.setPay_time(df.format(new Date()));
		int updateoeder = BisBusinessOrderService.updateoeder(bisBusinessOrder);
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		if (updateoeder>0) {
			String custom_user_id = (String) session.getAttribute("custom_user_id");
			bisCustomUser.setCustom_user_id(custom_user_id);
			bisCustomUser.setBeauty_num(beauty_num);
			BisCustomUserService.beauty_num(bisCustomUser);
			return String.format(result,"1","支付成功！");
		}else {
			return String.format(result,"0","支付失败！");
		}
		
	}
	/**
	 * 
	 * 简要说明：项目支付订单
	 * 编写者：张孝柱
	 * 创建时间：2018年9月27日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "projectPayOrder")
	@ResponseBody
	public String projectPayOrder(String order_id,String record_id,BisProjectRecord bisProjectRecord,BisBusinessOrder bisBusinessOrder){
		bisBusinessOrder.setOrder_id(order_id);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		bisBusinessOrder.setPay_time(df.format(new Date()));
		int updatep = BisBusinessOrderService.updatep(bisBusinessOrder);
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		if (updatep>0) {
			bisProjectRecord.setRecord_id(record_id);
			bisProjectRecord.setUse_time(new Date());
			BisProjectRecordService.up(bisProjectRecord);
			return String.format(result,"1","支付成功！");
		}else {
			return String.format(result,"0","支付失败！");
		}
	}
}
